﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Battery
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["battery"] = new Object();
myPlugins["battery"]["name"] = getLocalizedString2("mod_sysm_plugin_bat_name");
myPlugins["battery"]["onlyonce"] = true;
myPlugins["battery"]["newObject"] = function (NewSettings) { return new plugin_battery_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_battery_sensor(NewSettings) {
	// Required variables
	this.plugin = "battery";
	this.settings = NewSettings;
	this.edit = plugin_battery_edit;
	this.ReDraw = plugin_battery_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor_charching) this.settings.barcolor_charching = "#007FFF";
	if(!this.settings.barcolor_full) this.settings.barcolor_full = "#008000";
	if(!this.settings.barcolor_onbattery) this.settings.barcolor_onbattery = "#FF7F00";
	if(!this.settings.barcolor_low) this.settings.barcolor_low = "#FF0000";
	if(!this.settings.updspeed) this.settings.updspeed = 45;

	//The Sensor
	this.bat = new class_PercentBar(this.frame,0,0,getLocalizedString2("mod_sysm_plugin_bat_label"));
	this.bat.PercentColor(this.settings.color);


	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_battery_update.call(obj); };
	plugin_battery_update.call(obj);

	// Calculating sensore height (required!)
	this.frame.height = this.bat.background.vOffset + this.bat.background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_battery_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_bat_pref_colfull");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor_full;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_bat_pref_colcharching");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor_charching;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_bat_pref_colonbat");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor_onbattery;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_bat_pref_collowbat");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor_low;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(1,2,3,5,10,15,20,25,30,45,60);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.settings.barcolor_full = Drive_Edit_DialogResults[1];
			this.settings.barcolor_charching = Drive_Edit_DialogResults[2];
			this.settings.barcolor_onbattery = Drive_Edit_DialogResults[3];
			this.settings.barcolor_low = Drive_Edit_DialogResults[4];

			if ( this.settings.updspeed != Drive_Edit_DialogResults[5] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[5];
				this.timer.interval = this.settings.updspeed;
			}

			this.bat.PercentColor(this.settings.color);
			plugin_battery_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_battery_update() {
			// If requested it is posible to query all batteries.
			//system.batteryCount
			if( system.battery[0] ){
				if(system.battery[0].isCharging) {
					this.bat.PercentBarColor(this.settings.barcolor_charching);
					this.bat.PercentBarValue(system.battery[0].currentCapacity,Date2String(system.battery[0].timeToFullCharge));
				} else if(system.battery[0].powerSourceState=="AC Power") {
					this.bat.PercentBarColor(this.settings.barcolor_full);
					this.bat.PercentBarValue(system.battery[0].currentCapacity,getLocalizedString2("mod_sysm_plugin_bat_full"));
				} else {
					this.bat.PercentBarValue(system.battery[0].currentCapacity,Date2String(system.battery[0].timeToEmpty));
					if(system.battery[0].currentCapacity<30)
						this.bat.PercentBarColor(this.settings.barcolor_low);
					else
						this.bat.PercentBarColor(this.settings.barcolor_onbattery);
				}
			} else {
				this.bat.PercentBarValue(100,getLocalizedString2("mod_sysm_plugin_offline"));
				this.bat.PercentBarColor(this.settings.barcolor_low);
			}
		return true;
	}

	function Date2String(timeinminutes) {
		var timeDifference = timeinminutes;

		hRemain = Math.floor(timeDifference / 60); 
		timeDifference -= hRemain * 60;
		mRemain = timeDifference;
	
		if(mRemain < 10) return hRemain + ":0" + mRemain
		else  return hRemain + ":" + mRemain
	}

	function plugin_battery_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.bat.style = preferences.mod_sysmonitor_bar_style.value;
		this.bat.size = mod_sysmonitor_size;
		this.frame.height = this.bat.background.vOffset + this.bat.background.height;
		return true;
	}

}

// --------------------------------------------------------------------------------
// The END